//
//  TRoadMoreFooterView.m
//  TRoad2
//
//  Created by ZhangYanhua on 15/9/11.
//  Copyright (c) 2015年 Mach. All rights reserved.
//

#import "TRoadMoreFooterView.h"
#import "QWKitConfig.h"

#define TROAD_MORE_FOOTER_VIEW_HEIGHT    44.0f

#define MoreArrowImg_WIDTH 24
#define MoreArrowImg_HEIGHT 24

@implementation TRoadMoreFooterView

+ (TRoadMoreFooterView *)defaultMoreViewForTRoad
{
    TRoadMoreFooterView *moreHeaderView = nil;
    moreHeaderView = [[TRoadMoreFooterView alloc] initWithFrame:
                      CGRectMake(0, 0, kQWKitScreenWidth ,TROAD_MORE_FOOTER_VIEW_HEIGHT)];
    moreHeaderView.hidden = NO;
    moreHeaderView.backgroundColor = [UIColor clearColor];
    moreHeaderView.tag = 101;

    return moreHeaderView;
}

- (id)initWithFrame:(CGRect)frame
{
    frame.size.height = TROAD_MORE_FOOTER_VIEW_HEIGHT;
    self = [super initWithFrame:frame];
    if (self) {
        _moreLabel = [[UILabel alloc] initWithFrame:
                                    CGRectMake(0, 0, frame.size.width, TROAD_MORE_FOOTER_VIEW_HEIGHT)];
        _moreLabel.textColor = LIGHT_SUB_FONT_COLOR;
        _moreLabel.backgroundColor = [UIColor clearColor];
        _moreLabel.font = CUSTOM_BOLD_FONT(11.0f);
        _moreLabel.textAlignment = NSTextAlignmentCenter;
        _moreLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        _moreSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _moreSpinner.hidesWhenStopped = YES;
        _moreSpinner.transform = CGAffineTransformMakeScale(0.8f, 0.8f);
        _moreSpinner.color = [UIColor grayColor];

        [self addSubview:_moreLabel];
        [self addSubview:_moreSpinner];
        
        [self setupStrings];
    }
    return self;
}

- (void)setupStrings{
    self.textMorePull      = @"上拉加载更多...";
    self.textMoreRelease   = @"释放加载更多...";
    self.textMoreLoading   = @"正在载入更多...";
    self.textNoMoreContent = @"已全部展示";
}

- (CGFloat)viewHeight
{
    return TROAD_MORE_FOOTER_VIEW_HEIGHT;
}

- (void)pullToLoadMore
{
    self.moreLabel.text = self.textMorePull;
    [self.moreSpinner stopAnimating];
    [self setNeedsLayout];
}

- (void)releaseToLoadMore
{
    self.moreLabel.text = self.textMoreRelease;
    [self.moreSpinner stopAnimating];
    [self setNeedsLayout];
}

- (void)startMoreLoading
{
    self.moreLabel.text = nil;
    [self.moreSpinner startAnimating];
    [self setNeedsLayout];
}

- (void)stopMoreLoading
{
    self.moreLabel.text = self.textMorePull;
    [self.moreSpinner stopAnimating];
    [self setNeedsLayout];
}

- (void)showNoMoreContent
{
    self.moreLabel.text = self.textNoMoreContent;
    [self.moreSpinner stopAnimating];
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [_moreSpinner setCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2.0f, CGRectGetHeight(self.bounds) / 2.0f)];

    [_moreLabel setFrame:CGRectMake(20.0f, 0.0f,
                                    CGRectGetWidth(self.bounds) - 40.0f,
                                    CGRectGetHeight(self.bounds))];
}

@end
